package com.lijin.batchsys.framework.schedule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description: 运行日期类
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 * 
 */
public class RunDate {

	private static final String DATE_PARTTEN = "yyyyMMdd";

	private String runDateStr = null; // 运行日期字符串
	private int dayOfWeek;
	private int dayOfMonth;
	private int monthOfYear;
	private int dayOfYear;
	private int endDayOfMonth;
	private int endDayOfYear;

	public RunDate(String runDateStr) {
		this.runDateStr = runDateStr;

		Date runDate = null;
		try {
			SimpleDateFormat formatter = new SimpleDateFormat(DATE_PARTTEN);
			runDate = formatter.parse(runDateStr);
		} catch (ParseException e) {
			throw new RuntimeException("无法解析传入的日期字符串【" + runDateStr + "】", e);
		}

		Calendar c = Calendar.getInstance();
		c.setTime(runDate);

		dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
		dayOfYear = c.get(Calendar.DAY_OF_YEAR);
		monthOfYear = c.get(Calendar.MONTH);
		endDayOfMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		endDayOfYear = c.getActualMaximum(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 是否是周末
	 * 
	 * @return
	 */
	public boolean isWeekEnd() {
		return dayOfWeek == Calendar.SUNDAY;
	}

	/**
	 * 是否是月末
	 * 
	 * @return
	 */
	public boolean isMonthEnd() {
		return dayOfMonth == endDayOfMonth;
	}

	/**
	 * 是否是年末
	 * 
	 * @return
	 */
	public boolean isYearEnd() {
		return dayOfYear == endDayOfYear;
	}

	/**
	 * 是否是季度末
	 * 
	 * @return
	 */
	public boolean isSeasonEnd() {
		if (dayOfMonth == endDayOfMonth
				&& (monthOfYear == Calendar.MARCH 
						|| monthOfYear == Calendar.JUNE 
						|| monthOfYear == Calendar.SEPTEMBER 
						|| monthOfYear == Calendar.DECEMBER)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 是否是年中
	 * @return
	 */
	public boolean isHalfYearEnd() {
		if (dayOfMonth == endDayOfMonth && monthOfYear == Calendar.JUNE) {
			return true;
		}
		return false;
	}

	/**
	 * 根据运行周期判断是否可以运行
	 * 
	 * @param runPeriodType
	 * @return
	 */
	public boolean checkRun(String runPeriodType) {
		RunPeriod e = RunPeriod.parseByType(runPeriodType);
		switch (e) {
			case DAILY :
				return true;
			case WEEKLY :
				return isWeekEnd();
			case MONTHLY :
				return isMonthEnd();
			case HALF_YEARLY :
				return isHalfYearEnd();
			case YEARLY :
				return isYearEnd();
			default :
				return false;
		}
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Date[").append(this.runDateStr).append("]Week Day[").append(dayOfWeek).append(
				"]Day Of Month[").append(dayOfMonth).append("] End Day Of Month[").append(
				endDayOfMonth).append("]");
		if (this.isWeekEnd())
			sb.append(" [Week End]");
		if (this.isMonthEnd())
			sb.append(" [Month End]");
		if (this.isSeasonEnd())
			sb.append(" [Season End]");
		if (this.isHalfYearEnd())
			sb.append(" [Half Year End]");
		if (this.isYearEnd())
			sb.append(" [Year End]");
		return sb.toString();
	}
}
